package com.symaster.pdfcompose.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yinmiao
 * @version 2024/4/29
 */
public class Data {
    private final File configFile;
    private final JSONObject configJsonObj;

    public Data() {
        configFile = new File("app.json").getAbsoluteFile();
        if (configFile.isFile()) {
            configJsonObj = loadFile();
        } else {
            configJsonObj = new JSONObject();
        }
    }

    private JSONObject loadFile() {
        try (FileReader fileReader = new FileReader(configFile);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {

            StringBuilder configSb = new StringBuilder();
            bufferedReader.lines().forEach(line -> configSb.append(line).append(" "));

            String configStr = configSb.toString().trim();
            return JSONObject.parseObject(configStr);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public <T> List<T> getArray(String resultList, Class<T> resultItemEntityClass) {
        try {
            ArrayList<T> ret = new ArrayList<>();

            JSONArray jsonArray = configJsonObj.getJSONArray(resultList);
            if (jsonArray == null) {
                return ret;
            }

            for (int i = 0; i < jsonArray.size(); i++) {
                try {
                    T object = jsonArray.getObject(i, resultItemEntityClass);
                    ret.add(object);
                } catch (Exception ignored) {
                }
            }

            return ret;
        } catch (Exception e) {
            return null;
        }
    }

    public void delByIndex(String key, int index) {
        JSONArray jsonArray = configJsonObj.getJSONArray(key);
        if (jsonArray != null) {
            jsonArray.remove(index);
            configJsonObj.put(key, jsonArray);
            save();
        }
    }

    public void putArray(String key, Object obj) {

        JSONArray jsonArray = configJsonObj.getJSONArray(key);
        if (jsonArray == null) {
            jsonArray = new JSONArray();
        }

        jsonArray.add(obj);
        configJsonObj.put(key, jsonArray);

        save();
    }

    public void fastPutArray(String key, Object obj) {
        JSONArray jsonArray = configJsonObj.getJSONArray(key);
        if (jsonArray == null) {
            jsonArray = new JSONArray();
        }

        jsonArray.add(0, obj);
        configJsonObj.put(key, jsonArray);

        save();
    }

    private void save() {
        try (FileWriter fileWriter = new FileWriter(configFile);
             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
            String jsonString = configJsonObj.toJSONString();
            bufferedWriter.write(jsonString);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
